<?php
/**
 * 用户项目权限 Model
 *
 * PHP Version 5
 *
 * @package    Ecga
 * @subpackage Models
 * @author     blue5tar <blue5tar@163.com>
 * @copyright  2010 Xingchangxinda Inc.
 * @version    SVN:$Id: masters.php 3051 2011-01-11 13:06:18Z sunbo $
 */

namespace Project\Apps\Ecga\Models;


/**
 * 用户项目权限 Model
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    blue5tar <blue5tar@163.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: 0.1
 */
class UserProjectPrivs extends \Project\Models\Abstraction\Ecga\UserProjectPrivs
{
    /**
     * 按用户Id获取
     * 
     * @param int $userId 用户Id
     * 
     * @return object
     */
    public function getByUserId($userId)
    {
        return $this->select()
            ->where('userId', '=', $userId)
            ->fetch();
    }
    /**
     * 根据用户ID获取其有权限的项目ID
     * 
     * @param int $userId 用户Id
     * 
     * @author Wanggeng <wanggeng@ec3s.com> 
     * @return array
     * 
     */
    public function getProjectByUserId($userId)
    {
        $result = $this->select('projectId')
            ->where('userId', '=', $userId)
            ->fetch();
        $result = iterator_to_array($result);
        foreach ($result as $value) {
            $return[] .= $value['projectId'];
        }
        return $return;
    }
    /**
     * 按用户Id获取数组结构
     * 
     * @param int $userId 用户Id
     * 
     * @return array
     */
    public function getByUserIdToArray($userId)
    {
        $result = $this->select('*')
            ->where('userId', '=', $userId)
            ->fetch();
        $result = iterator_to_array($result);
        $return = array();
        foreach ($result as $key => $value) {
            $return[$value['projectId']]['privs'] = unserialize($value['privs']); 
        }
        return $return;
    }
}